﻿using System;
using System.Data;
using olierFrm.WebFormDev;
using olierFrm.WebFormDev.Cookie;
using olierFrm.WebFormDev.StdQuery;
using olierFrm.DataBase.FrameWork.StdQuery;
using olierFrm.DataBase;
using olierFrm.Extensions.EDataTable;
using DevExpress.Web.ASPxGridView;
using System.Drawing;
using System.Linq;

namespace WebFormDev.StdQueryPack
{
    public partial class GridPage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);

            hfThemeName.Value = CTheme.Theme;
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            if (StdQuery.QueryName == null) { return; }

            StdQuery.Session.InitGridSession();

            //没有QueryName只显示菜单，为了重复利用该页面（系统首页和StdQuery）

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var dbStdQuery = new StdQuerySQL(StdQuery.QueryName);


            foreach (var p in dbStdQuery.StdQueryParams)
            {
                if (p.ParamType.ToLower() == "enddate" && Request.QueryString[p.ParamName] != null && Request.QueryString[p.ParamName] == "")
                    dbStdQuery.SetParam(p.ParamName, "2100-1-1");
                else if (p.ParamType.ToLower() == "date" && Request.QueryString[p.ParamName] != null && Request.QueryString[p.ParamName] == "")
                    dbStdQuery.SetParam(p.ParamName, "1900-1-1");
                else dbStdQuery.SetParam(p.ParamName, Request.QueryString[p.ParamName]);
            }
            dbStdQuery.AddSystemParams();

            //var su = (SessionUser)Session[SessionUser.SessionID];

            DataTable dtSource;
            try
            {
                dtSource = dbStdQuery.ExecuteSQL();
            }
            catch (Exception ex)
            {
                Response.Clear();
                Response.Write(ex.Message);
                Response.End();
                return;
            }
            dtSource.AddDataRowIndex();

            //var gg = new ASPxGridView() { DataSource = dtSource };
            //gg.Settings.ShowHorizontalScrollBar = true;
            //gg.Settings.ShowVerticalScrollBar = true;
            //gg.Width = 600;
            //gg.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Control;
            //gg.Styles.EmptyDataRow.BackColor = Color.Transparent;

            //gg.DataBind();
            //form1.Controls.Add(gg);
            //return;
            //dtSource.BindCaptionToDataTable();

            StdQuerySource sss = new StdQuerySource(dtSource, dbStdQuery);

            var gridMaster = sss.CreateGridView("StdQueryGrid", 30);
            gridMaster.ClientSideEvents.EndCallback = "function(s,e){parent.parent.parent.parent.$().ResizeAll();InitGrid();}";
            //gridMaster.Width = int.Parse(Request.QueryString["hfThisWidth"]);
            //for (int i = 0; i < gridMaster.VisibleRowCount; i++)
            //{
            //    for (int j = 0; j < gridMaster.VisibleColumns.Count; j++)
            //    {
            //        var ow = gridMaster.VisibleColumns[j].Width;
            //        //dtSource.Rows
            //    }
            //}
            gridMaster.Settings.UseFixedTableLayout = false;
            form1.Controls.Add(gridMaster);
        }


    }
}